package horarios;


/*Copyright [2011] [Marco Aurelio Pontes]

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
 */
import javax.microedition.midlet.*;
import javax.microedition.lcdui.*;
import net.sourceforge.floggy.persistence.FloggyException;
import net.sourceforge.floggy.persistence.ObjectSet;
import net.sourceforge.floggy.persistence.PersistableManager;
import java.util.Calendar;
import java.util.Date;

/**
 * @author Lais Zanfolim
 */
public class HorariosMIDlet extends MIDlet implements CommandListener, ItemStateListener {

    private boolean midletPaused = false;
    Calendar c1 = Calendar.getInstance();
    Calendar c2 = Calendar.getInstance();
    Date horarioComp = new Date();
    //<editor-fold defaultstate="collapsed" desc=" Generated Fields ">//GEN-BEGIN:|fields|0|
    private Command cmdSalvar;
    private Command cmdVoltar;
    private Command cmdSairLista;
    private Command cmdAddHorarios;
    private Command cmdFiltro;
    private Command Pesquisar;
    private Command cmdAddOnibus;
    private Command cmdSalvarOnibus;
    private Form addHorario;
    private DateField textFieldHoraSaida;
    private ChoiceGroup choiceTipo;
    private ChoiceGroup popupOnibus;
    private StringItem textFieldOrigem;
    private Spacer spacer;
    private StringItem textFieldDestino;
    private Alert alert;
    private List listaHorarios;
    private Form dadosFiltro;
    private ChoiceGroup choiceGroup;
    private ChoiceGroup choiceGroup1;
    private ChoiceGroup choiceGroup2;
    private Spacer spacer1;
    private StringItem stringItem;
    private DateField dateField;
    private DateField dateField1;
    private Form addOnibus;
    private TextField fieldNomeOnibus;
    private TextField fieldOrigem;
    private TextField fieldDestino;
    private Alert alert1;
    //</editor-fold>//GEN-END:|fields|0|

    /**
     * The HelloMIDlet constructor.
     */
    public HorariosMIDlet() {
    }

    //<editor-fold defaultstate="collapsed" desc=" Generated Methods ">//GEN-BEGIN:|methods|0|
    //</editor-fold>//GEN-END:|methods|0|
    //<editor-fold defaultstate="collapsed" desc=" Generated Method: initialize ">//GEN-BEGIN:|0-initialize|0|0-preInitialize
    /**
     * Initilizes the application.
     * It is called only once when the MIDlet is started. The method is called before the <code>startMIDlet</code> method.
     */
    private void initialize() {//GEN-END:|0-initialize|0|0-preInitialize
        // write pre-initialize user code here
//GEN-LINE:|0-initialize|1|0-postInitialize
        // write post-initialize user code here
    }//GEN-BEGIN:|0-initialize|2|
    //</editor-fold>//GEN-END:|0-initialize|2|

    //<editor-fold defaultstate="collapsed" desc=" Generated Method: startMIDlet ">//GEN-BEGIN:|3-startMIDlet|0|3-preAction
    /**
     * Performs an action assigned to the Mobile Device - MIDlet Started point.
     */
    public void startMIDlet() {//GEN-END:|3-startMIDlet|0|3-preAction
        // write pre-action user code here
        switchDisplayable(null, getListaHorarios());//GEN-LINE:|3-startMIDlet|1|3-postAction
        // write post-action user code here
    }//GEN-BEGIN:|3-startMIDlet|2|
    //</editor-fold>//GEN-END:|3-startMIDlet|2|

    //<editor-fold defaultstate="collapsed" desc=" Generated Method: resumeMIDlet ">//GEN-BEGIN:|4-resumeMIDlet|0|4-preAction
    /**
     * Performs an action assigned to the Mobile Device - MIDlet Resumed point.
     */
    public void resumeMIDlet() {//GEN-END:|4-resumeMIDlet|0|4-preAction
        // write pre-action user code here
//GEN-LINE:|4-resumeMIDlet|1|4-postAction
        // write post-action user code here
    }//GEN-BEGIN:|4-resumeMIDlet|2|
    //</editor-fold>//GEN-END:|4-resumeMIDlet|2|

    //<editor-fold defaultstate="collapsed" desc=" Generated Method: switchDisplayable ">//GEN-BEGIN:|5-switchDisplayable|0|5-preSwitch
    /**
     * Switches a current displayable in a display. The <code>display</code> instance is taken from <code>getDisplay</code> method. This method is used by all actions in the design for switching displayable.
     * @param alert the Alert which is temporarily set to the display; if <code>null</code>, then <code>nextDisplayable</code> is set immediately
     * @param nextDisplayable the Displayable to be set
     */
    public void switchDisplayable(Alert alert, Displayable nextDisplayable) {//GEN-END:|5-switchDisplayable|0|5-preSwitch
        // write pre-switch user code here
        Display display = getDisplay();//GEN-BEGIN:|5-switchDisplayable|1|5-postSwitch
        if (alert == null) {
            display.setCurrent(nextDisplayable);
        } else {
            display.setCurrent(alert, nextDisplayable);
        }//GEN-END:|5-switchDisplayable|1|5-postSwitch
        // write post-switch user code here
    }//GEN-BEGIN:|5-switchDisplayable|2|
    //</editor-fold>//GEN-END:|5-switchDisplayable|2|

    //<editor-fold defaultstate="collapsed" desc=" Generated Method: commandAction for Displayables ">//GEN-BEGIN:|7-commandAction|0|7-preCommandAction
    /**
     * Called by a system to indicated that a command has been invoked on a particular displayable.
     * @param command the Command that was invoked
     * @param displayable the Displayable where the command was invoked
     */
    public void commandAction(Command command, Displayable displayable) {//GEN-END:|7-commandAction|0|7-preCommandAction
        // write pre-action user code here
        if (displayable == addHorario) {//GEN-BEGIN:|7-commandAction|1|25-preAction
            if (command == cmdSalvar) {//GEN-END:|7-commandAction|1|25-preAction
                if (popupOnibus.getString(popupOnibus.getSelectedIndex()).equals("") || textFieldOrigem.getText().equals("")) {
                    getAlert().setString("Algum campo esta vazio! Preencha corretamente");
                } else {
                    adicionarHorario();
                    getAlert().setString("Os dados foram inseridos com sucesso!");
                    popupOnibus.setSelectedIndex(0, true);
                    textFieldOrigem.setText("");
                    textFieldDestino.setText("");
                    getChoiceTipo().setSelectedIndex(0, true);
                }
                switchDisplayable(getAlert(), getAddHorario());//GEN-LINE:|7-commandAction|2|25-postAction
                // write post-action user code here
            } else if (command == cmdVoltar) {//GEN-LINE:|7-commandAction|3|95-preAction
                // write pre-action user code here
                switchDisplayable(null, getListaHorarios());//GEN-LINE:|7-commandAction|4|95-postAction
                // write post-action user code here
            }//GEN-BEGIN:|7-commandAction|5|82-preAction
        } else if (displayable == addOnibus) {
            if (command == cmdSalvarOnibus) {//GEN-END:|7-commandAction|5|82-preAction
                // write pre-action user code here
                switchDisplayable(getAlert1(), getListaHorarios());//GEN-LINE:|7-commandAction|6|82-postAction
                // write post-action user code here
                if (fieldNomeOnibus.getString().equals("") || fieldOrigem.getString().equals("") || fieldDestino.getString().equals("")) {
                    getAlert().setString("Algum campo esta vazio! Preencha corretamente");
                } else {
                    adicionarOnibus();
                    fieldNomeOnibus.setString("");
                    fieldOrigem.setString("");
                    fieldDestino.setString("");
                    getAlert1().setString("Os dados foram gravados com sucesso!");
                }
            } else if (command == cmdVoltar) {//GEN-LINE:|7-commandAction|7|99-preAction
                // write pre-action user code here
                switchDisplayable(null, getListaHorarios());//GEN-LINE:|7-commandAction|8|99-postAction
                // write post-action user code here
            }//GEN-BEGIN:|7-commandAction|9|30-preAction
        } else if (displayable == alert) {
            if (command == cmdVoltar) {//GEN-END:|7-commandAction|9|30-preAction
                // write pre-action user code here
                switchDisplayable(null, getAddHorario());//GEN-LINE:|7-commandAction|10|30-postAction
                // write post-action user code here
            }//GEN-BEGIN:|7-commandAction|11|101-preAction
        } else if (displayable == alert1) {
            if (command == cmdVoltar) {//GEN-END:|7-commandAction|11|101-preAction
                // write pre-action user code here
                switchDisplayable(null, getListaHorarios());//GEN-LINE:|7-commandAction|12|101-postAction
                // write post-action user code here
            }//GEN-BEGIN:|7-commandAction|13|66-preAction
        } else if (displayable == dadosFiltro) {
            if (command == Pesquisar) {//GEN-END:|7-commandAction|13|66-preAction
                // write pre-action user code here
                switchDisplayable(null, getListaHorarios());//GEN-LINE:|7-commandAction|14|66-postAction
                // write post-action user code here

            } else if (command == cmdVoltar) {//GEN-LINE:|7-commandAction|15|97-preAction
                // write pre-action user code here
                switchDisplayable(null, getListaHorarios());//GEN-LINE:|7-commandAction|16|97-postAction
                // write post-action user code here
            }//GEN-BEGIN:|7-commandAction|17|35-preAction
        } else if (displayable == listaHorarios) {
            if (command == List.SELECT_COMMAND) {//GEN-END:|7-commandAction|17|35-preAction
                // write pre-action user code here
                listaHorariosAction();//GEN-LINE:|7-commandAction|18|35-postAction
                // write post-action user code here

                //listarDados(filtro);
            } else if (command == cmdAddHorarios) {//GEN-LINE:|7-commandAction|19|48-preAction
                // write pre-action user code here
                switchDisplayable(null, getAddHorario());//GEN-LINE:|7-commandAction|20|48-postAction
                // write post-action user code here
            } else if (command == cmdAddOnibus) {//GEN-LINE:|7-commandAction|21|75-preAction
                // write pre-action user code here
                switchDisplayable(null, getAddOnibus());//GEN-LINE:|7-commandAction|22|75-postAction
                // write post-action user code here
            } else if (command == cmdFiltro) {//GEN-LINE:|7-commandAction|23|58-preAction
                // write pre-action user code here
                switchDisplayable(null, getDadosFiltro());//GEN-LINE:|7-commandAction|24|58-postAction
                // write post-action user code here
                //getListaHorarios().deleteAll();
                //filtro = "Norte";


            } else if (command == cmdSairLista) {//GEN-LINE:|7-commandAction|25|38-preAction
                // write pre-action user code here
                exitMIDlet();//GEN-LINE:|7-commandAction|26|38-postAction
                // write post-action user code here
            }//GEN-BEGIN:|7-commandAction|27|7-postCommandAction
        }//GEN-END:|7-commandAction|27|7-postCommandAction
        // write post-action user code here
    }//GEN-BEGIN:|7-commandAction|28|
    //</editor-fold>//GEN-END:|7-commandAction|28|

    //</editor-fold>
    //</editor-fold>

    //</editor-fold>
    //</editor-fold>

    //<editor-fold defaultstate="collapsed" desc=" Generated Getter: addHorario ">//GEN-BEGIN:|14-getter|0|14-preInit
    /**
     * Returns an initiliazed instance of addHorario component.
     * @return the initialized component instance
     */
    public Form getAddHorario() {
        if (addHorario == null) {//GEN-END:|14-getter|0|14-preInit
            // write pre-init user code here
            addHorario = new Form("Novo Hor\u00E1rio de \u00D4nibus", new Item[] { getPopupOnibus(), getTextFieldOrigem(), getTextFieldDestino(), getTextFieldHoraSaida(), getChoiceTipo(), getSpacer() });//GEN-BEGIN:|14-getter|1|14-postInit
            addHorario.addCommand(getCmdSalvar());
            addHorario.addCommand(getCmdVoltar());
            addHorario.setCommandListener(this);//GEN-END:|14-getter|1|14-postInit
            // write post-init user code here
            addHorario.setItemStateListener(this);
        }//GEN-BEGIN:|14-getter|2|
        return addHorario;
    }
    //</editor-fold>//GEN-END:|14-getter|2|
    //</editor-fold>

    //<editor-fold defaultstate="collapsed" desc=" Generated Getter: cmdSalvar ">//GEN-BEGIN:|24-getter|0|24-preInit
    /**
     * Returns an initiliazed instance of cmdSalvar component.
     * @return the initialized component instance
     */
    public Command getCmdSalvar() {
        if (cmdSalvar == null) {//GEN-END:|24-getter|0|24-preInit
            // write pre-init user code here
            cmdSalvar = new Command("Inserir", Command.OK, 0);//GEN-LINE:|24-getter|1|24-postInit
            // write post-init user code here
        }//GEN-BEGIN:|24-getter|2|
        return cmdSalvar;
    }
    //</editor-fold>//GEN-END:|24-getter|2|

    //<editor-fold defaultstate="collapsed" desc=" Generated Getter: alert ">//GEN-BEGIN:|28-getter|0|28-preInit
    /**
     * Returns an initiliazed instance of alert component.
     * @return the initialized component instance
     */
    public Alert getAlert() {
        if (alert == null) {//GEN-END:|28-getter|0|28-preInit
            // write pre-init user code here
            alert = new Alert("Aviso!");//GEN-BEGIN:|28-getter|1|28-postInit
            alert.addCommand(getCmdVoltar());
            alert.setCommandListener(this);
            alert.setTimeout(Alert.FOREVER);//GEN-END:|28-getter|1|28-postInit
            // write post-init user code here
        }//GEN-BEGIN:|28-getter|2|
        return alert;
    }
    //</editor-fold>//GEN-END:|28-getter|2|
    //</editor-fold>
    //</editor-fold>

    //<editor-fold defaultstate="collapsed" desc=" Generated Getter: cmdSairLista ">//GEN-BEGIN:|37-getter|0|37-preInit
    /**
     * Returns an initiliazed instance of cmdSairLista component.
     * @return the initialized component instance
     */
    public Command getCmdSairLista() {
        if (cmdSairLista == null) {//GEN-END:|37-getter|0|37-preInit
            // write pre-init user code here
            cmdSairLista = new Command("Voltar", Command.EXIT, 0);//GEN-LINE:|37-getter|1|37-postInit
            // write post-init user code here
        }//GEN-BEGIN:|37-getter|2|
        return cmdSairLista;
    }
    //</editor-fold>//GEN-END:|37-getter|2|
    //</editor-fold>

    //<editor-fold defaultstate="collapsed" desc=" Generated Getter: listaHorarios ">//GEN-BEGIN:|33-getter|0|33-preInit
    /**
     * Returns an initiliazed instance of listaHorarios component.
     * @return the initialized component instance
     */
    public List getListaHorarios() {
        if (listaHorarios == null) {//GEN-END:|33-getter|0|33-preInit
            // write pre-init user code here

            listaHorarios = new List("Hor\u00E1rios", Choice.IMPLICIT);//GEN-BEGIN:|33-getter|1|33-postInit
            listaHorarios.addCommand(getCmdSairLista());
            listaHorarios.addCommand(getCmdAddHorarios());
            listaHorarios.addCommand(getCmdFiltro());
            listaHorarios.addCommand(getCmdAddOnibus());
            listaHorarios.setCommandListener(this);
            listaHorarios.setFitPolicy(Choice.TEXT_WRAP_DEFAULT);
            listaHorarios.setSelectCommand(null);//GEN-END:|33-getter|1|33-postInit
            // write post-init user code here
            listarDados(new Filtro());
        } else {
            listaHorarios.deleteAll();
            listarDados(new Filtro());
        }//GEN-BEGIN:|33-getter|2|
        return listaHorarios;
    }
    //</editor-fold>//GEN-END:|33-getter|2|

    //<editor-fold defaultstate="collapsed" desc=" Generated Method: listaHorariosAction ">//GEN-BEGIN:|33-action|0|33-preAction
    /**
     * Performs an action assigned to the selected list element in the listaHorarios component.
     */
    public void listaHorariosAction() {//GEN-END:|33-action|0|33-preAction
        // enter pre-action user code here
        String __selectedString = getListaHorarios().getString(getListaHorarios().getSelectedIndex());//GEN-LINE:|33-action|1|33-postAction
        // enter post-action user code here
    }//GEN-BEGIN:|33-action|2|
    //</editor-fold>//GEN-END:|33-action|2|
    //</editor-fold>
    //</editor-fold>

    //<editor-fold defaultstate="collapsed" desc=" Generated Getter: textFieldHoraSaida ">//GEN-BEGIN:|41-getter|0|41-preInit
    /**
     * Returns an initiliazed instance of textFieldHoraSaida component.
     * @return the initialized component instance
     */
    public DateField getTextFieldHoraSaida() {
        if (textFieldHoraSaida == null) {//GEN-END:|41-getter|0|41-preInit
            // write pre-init user code here
            textFieldHoraSaida = new DateField("Hora de Sa\u00EDda", DateField.TIME);//GEN-BEGIN:|41-getter|1|41-postInit
            textFieldHoraSaida.setPreferredSize(-1, -1);
            textFieldHoraSaida.setDate(new java.util.Date(54000000l));//GEN-END:|41-getter|1|41-postInit
            // write post-init user code here
        }//GEN-BEGIN:|41-getter|2|
        return textFieldHoraSaida;
    }
    //</editor-fold>//GEN-END:|41-getter|2|
    //</editor-fold>

    //</editor-fold>
    //</editor-fold>
    //</editor-fold>
    //</editor-fold>
    //</editor-fold>
    //<editor-fold defaultstate="collapsed" desc=" Generated Getter: cmdAddHorarios ">//GEN-BEGIN:|47-getter|0|47-preInit
    /**
     * Returns an initiliazed instance of cmdAddHorarios component.
     * @return the initialized component instance
     */
    public Command getCmdAddHorarios() {
        if (cmdAddHorarios == null) {//GEN-END:|47-getter|0|47-preInit
            // write pre-init user code here
            cmdAddHorarios = new Command("Novo Hor\u00E1rio", Command.OK, 0);//GEN-LINE:|47-getter|1|47-postInit
            // write post-init user code here
        }//GEN-BEGIN:|47-getter|2|
        return cmdAddHorarios;
    }
    //</editor-fold>//GEN-END:|47-getter|2|
    //</editor-fold>

    //<editor-fold defaultstate="collapsed" desc=" Generated Getter: choiceTipo ">//GEN-BEGIN:|52-getter|0|52-preInit
    /**
     * Returns an initiliazed instance of choiceTipo component.
     * @return the initialized component instance
     */
    public ChoiceGroup getChoiceTipo() {
        if (choiceTipo == null) {//GEN-END:|52-getter|0|52-preInit
            // write pre-init user code here
            choiceTipo = new ChoiceGroup("V\u00E1lido em: ", Choice.POPUP);//GEN-BEGIN:|52-getter|1|52-postInit
            choiceTipo.append("Dias \u00FAteis", null);
            choiceTipo.append("S\u00E1bados", null);
            choiceTipo.append("Domingos e feriados", null);
            choiceTipo.setFitPolicy(Choice.TEXT_WRAP_DEFAULT);
            choiceTipo.setSelectedFlags(new boolean[] { false, false, false });//GEN-END:|52-getter|1|52-postInit
            // write post-init user code here
        }//GEN-BEGIN:|52-getter|2|
        return choiceTipo;
    }
    //</editor-fold>//GEN-END:|52-getter|2|
    //</editor-fold>
    //</editor-fold>

    //</editor-fold>
    //</editor-fold>
    //</editor-fold>
    //</editor-fold>
    //<editor-fold defaultstate="collapsed" desc=" Generated Getter: cmdFiltro ">//GEN-BEGIN:|57-getter|0|57-preInit
    /**
     * Returns an initiliazed instance of cmdFiltro component.
     * @return the initialized component instance
     */
    public Command getCmdFiltro() {
        if (cmdFiltro == null) {//GEN-END:|57-getter|0|57-preInit
            // write pre-init user code here
            cmdFiltro = new Command("Pesquisar \u00D4nibus", Command.OK, 0);//GEN-LINE:|57-getter|1|57-postInit
            // write post-init user code here
        }//GEN-BEGIN:|57-getter|2|
        return cmdFiltro;
    }
    //</editor-fold>//GEN-END:|57-getter|2|

    //<editor-fold defaultstate="collapsed" desc=" Generated Getter: Pesquisar ">//GEN-BEGIN:|65-getter|0|65-preInit
    /**
     * Returns an initiliazed instance of Pesquisar component.
     * @return the initialized component instance
     */
    public Command getPesquisar() {
        if (Pesquisar == null) {//GEN-END:|65-getter|0|65-preInit
            // write pre-init user code here
            Pesquisar = new Command("Filtrar Lista de \u00F4nibus", Command.OK, 0);//GEN-LINE:|65-getter|1|65-postInit
            // write post-init user code here
        }//GEN-BEGIN:|65-getter|2|
        return Pesquisar;
    }
    //</editor-fold>//GEN-END:|65-getter|2|

    //<editor-fold defaultstate="collapsed" desc=" Generated Getter: dadosFiltro ">//GEN-BEGIN:|63-getter|0|63-preInit
    /**
     * Returns an initiliazed instance of dadosFiltro component.
     * @return the initialized component instance
     */
    public Form getDadosFiltro() {
        if (dadosFiltro == null) {//GEN-END:|63-getter|0|63-preInit
            // write pre-init user code here
            dadosFiltro = new Form("Filtrar pelos seguintes crit\u00E9rios:", new Item[] { getChoiceGroup(), getChoiceGroup1(), getChoiceGroup2(), getSpacer1(), getStringItem(), getDateField(), getDateField1() });//GEN-BEGIN:|63-getter|1|63-postInit
            dadosFiltro.addCommand(getPesquisar());
            dadosFiltro.addCommand(getCmdVoltar());
            dadosFiltro.setCommandListener(this);//GEN-END:|63-getter|1|63-postInit
            // write post-init user code here
        }//GEN-BEGIN:|63-getter|2|
        return dadosFiltro;
    }
    //</editor-fold>//GEN-END:|63-getter|2|

    //<editor-fold defaultstate="collapsed" desc=" Generated Getter: choiceGroup ">//GEN-BEGIN:|72-getter|0|72-preInit
    /**
     * Returns an initiliazed instance of choiceGroup component.
     * @return the initialized component instance
     */
    public ChoiceGroup getChoiceGroup() {
        if (choiceGroup == null) {//GEN-END:|72-getter|0|72-preInit
            // write pre-init user code here
            choiceGroup = new ChoiceGroup("Hor\u00E1rio v\u00E1lido em:", Choice.POPUP);//GEN-BEGIN:|72-getter|1|72-postInit
            choiceGroup.append("Dias \u00DAteis", null);
            choiceGroup.append("S\u00E1bados", null);
            choiceGroup.append("Domingos e feriados", null);
            choiceGroup.setSelectedFlags(new boolean[] { false, false, false });//GEN-END:|72-getter|1|72-postInit
            // write post-init user code here
        }//GEN-BEGIN:|72-getter|2|
        return choiceGroup;
    }
    //</editor-fold>//GEN-END:|72-getter|2|

    //<editor-fold defaultstate="collapsed" desc=" Generated Getter: popupOnibus ">//GEN-BEGIN:|73-getter|0|73-preInit
    /**
     * Returns an initiliazed instance of popupOnibus component.
     * @return the initialized component instance
     */
    public ChoiceGroup getPopupOnibus() {
        if (popupOnibus == null) {//GEN-END:|73-getter|0|73-preInit
            // write pre-init user code here
            popupOnibus = new ChoiceGroup("Nome do \u00D4nibus", Choice.POPUP);//GEN-BEGIN:|73-getter|1|73-postInit
            popupOnibus.setLayout(ImageItem.LAYOUT_DEFAULT);
            popupOnibus.setFitPolicy(Choice.TEXT_WRAP_DEFAULT);
            popupOnibus.setSelectedFlags(new boolean[] {  });//GEN-END:|73-getter|1|73-postInit
            // write post-init user code here

            ObjectSet os = null;
            try {
                os = PersistableManager.getInstance().find(Onibus.class, null, null);

                for (int i = 0; i < os.size(); i++) {
                    Onibus onibus = (Onibus) os.get(i); //obtem as instancias do banco
                    popupOnibus.append(onibus.getNomeOnibus(), null);
                }
            } catch (FloggyException ex) {
            }
        }//GEN-BEGIN:|73-getter|2|
        return popupOnibus;
    }
    //</editor-fold>//GEN-END:|73-getter|2|
    //</editor-fold>

    //<editor-fold defaultstate="collapsed" desc=" Generated Getter: cmdAddOnibus ">//GEN-BEGIN:|74-getter|0|74-preInit
    /**
     * Returns an initiliazed instance of cmdAddOnibus component.
     * @return the initialized component instance
     */
    public Command getCmdAddOnibus() {
        if (cmdAddOnibus == null) {//GEN-END:|74-getter|0|74-preInit
            // write pre-init user code here
            cmdAddOnibus = new Command("Novo \u00D4nibus", Command.OK, 0);//GEN-LINE:|74-getter|1|74-postInit
            // write post-init user code here
        }//GEN-BEGIN:|74-getter|2|
        return cmdAddOnibus;
    }
    //</editor-fold>//GEN-END:|74-getter|2|

    //<editor-fold defaultstate="collapsed" desc=" Generated Getter: cmdSalvarOnibus ">//GEN-BEGIN:|81-getter|0|81-preInit
    /**
     * Returns an initiliazed instance of cmdSalvarOnibus component.
     * @return the initialized component instance
     */
    public Command getCmdSalvarOnibus() {
        if (cmdSalvarOnibus == null) {//GEN-END:|81-getter|0|81-preInit
            // write pre-init user code here
            cmdSalvarOnibus = new Command("Salvar", Command.OK, 0);//GEN-LINE:|81-getter|1|81-postInit
            // write post-init user code here
        }//GEN-BEGIN:|81-getter|2|
        return cmdSalvarOnibus;
    }
    //</editor-fold>//GEN-END:|81-getter|2|

    //<editor-fold defaultstate="collapsed" desc=" Generated Getter: addOnibus ">//GEN-BEGIN:|76-getter|0|76-preInit
    /**
     * Returns an initiliazed instance of addOnibus component.
     * @return the initialized component instance
     */
    public Form getAddOnibus() {
        if (addOnibus == null) {//GEN-END:|76-getter|0|76-preInit
            // write pre-init user code here
            addOnibus = new Form("Criar Nova Linha", new Item[] { getFieldNomeOnibus(), getFieldOrigem(), getFieldDestino() });//GEN-BEGIN:|76-getter|1|76-postInit
            addOnibus.addCommand(getCmdSalvarOnibus());
            addOnibus.addCommand(getCmdVoltar());
            addOnibus.setCommandListener(this);//GEN-END:|76-getter|1|76-postInit
            // write post-init user code here
        }//GEN-BEGIN:|76-getter|2|
        return addOnibus;
    }
    //</editor-fold>//GEN-END:|76-getter|2|

    //<editor-fold defaultstate="collapsed" desc=" Generated Getter: fieldNomeOnibus ">//GEN-BEGIN:|78-getter|0|78-preInit
    /**
     * Returns an initiliazed instance of fieldNomeOnibus component.
     * @return the initialized component instance
     */
    public TextField getFieldNomeOnibus() {
        if (fieldNomeOnibus == null) {//GEN-END:|78-getter|0|78-preInit
            // write pre-init user code here
            fieldNomeOnibus = new TextField("Nome do \u00D4nibus", null, 32, TextField.ANY);//GEN-LINE:|78-getter|1|78-postInit
            // write post-init user code here
        }//GEN-BEGIN:|78-getter|2|
        return fieldNomeOnibus;
    }
    //</editor-fold>//GEN-END:|78-getter|2|

    //<editor-fold defaultstate="collapsed" desc=" Generated Getter: fieldOrigem ">//GEN-BEGIN:|79-getter|0|79-preInit
    /**
     * Returns an initiliazed instance of fieldOrigem component.
     * @return the initialized component instance
     */
    public TextField getFieldOrigem() {
        if (fieldOrigem == null) {//GEN-END:|79-getter|0|79-preInit
            // write pre-init user code here
            fieldOrigem = new TextField("Terminal de Origem", null, 32, TextField.ANY);//GEN-LINE:|79-getter|1|79-postInit
            // write post-init user code here
        }//GEN-BEGIN:|79-getter|2|
        return fieldOrigem;
    }
    //</editor-fold>//GEN-END:|79-getter|2|

    //<editor-fold defaultstate="collapsed" desc=" Generated Getter: fieldDestino ">//GEN-BEGIN:|80-getter|0|80-preInit
    /**
     * Returns an initiliazed instance of fieldDestino component.
     * @return the initialized component instance
     */
    public TextField getFieldDestino() {
        if (fieldDestino == null) {//GEN-END:|80-getter|0|80-preInit
            // write pre-init user code here
            fieldDestino = new TextField("Destino", null, 32, TextField.ANY);//GEN-LINE:|80-getter|1|80-postInit
            // write post-init user code here
        }//GEN-BEGIN:|80-getter|2|
        return fieldDestino;
    }
    //</editor-fold>//GEN-END:|80-getter|2|

    //<editor-fold defaultstate="collapsed" desc=" Generated Getter: spacer ">//GEN-BEGIN:|88-getter|0|88-preInit
    /**
     * Returns an initiliazed instance of spacer component.
     * @return the initialized component instance
     */
    public Spacer getSpacer() {
        if (spacer == null) {//GEN-END:|88-getter|0|88-preInit
            // write pre-init user code here
            spacer = new Spacer(16, 1);//GEN-LINE:|88-getter|1|88-postInit
            // write post-init user code here
        }//GEN-BEGIN:|88-getter|2|
        return spacer;
    }
    //</editor-fold>//GEN-END:|88-getter|2|
    //</editor-fold>
    //</editor-fold>

    //<editor-fold defaultstate="collapsed" desc=" Generated Getter: textFieldOrigem ">//GEN-BEGIN:|89-getter|0|89-preInit
    /**
     * Returns an initiliazed instance of textFieldOrigem component.
     * @return the initialized component instance
     */
    public StringItem getTextFieldOrigem() {
        if (textFieldOrigem == null) {//GEN-END:|89-getter|0|89-preInit
            // write pre-init user code here
            textFieldOrigem = new StringItem("Terminal de Sa\u00EDda", "Escolha um \u00F4nibus");//GEN-LINE:|89-getter|1|89-postInit
            // write post-init user code here
            //stringItem.setText();
        }//GEN-BEGIN:|89-getter|2|
        return textFieldOrigem;
    }
    //</editor-fold>//GEN-END:|89-getter|2|
    //</editor-fold>

    //<editor-fold defaultstate="collapsed" desc=" Generated Getter: textFieldDestino ">//GEN-BEGIN:|90-getter|0|90-preInit
    /**
     * Returns an initiliazed instance of textFieldDestino component.
     * @return the initialized component instance
     */
    public StringItem getTextFieldDestino() {
        if (textFieldDestino == null) {//GEN-END:|90-getter|0|90-preInit
            // write pre-init user code here
            textFieldDestino = new StringItem("Terminal de Destino", "Escolha um \u00F4nibus");//GEN-LINE:|90-getter|1|90-postInit
            // write post-init user code here
        }//GEN-BEGIN:|90-getter|2|
        return textFieldDestino;
    }
    //</editor-fold>//GEN-END:|90-getter|2|

    //<editor-fold defaultstate="collapsed" desc=" Generated Getter: alert1 ">//GEN-BEGIN:|94-getter|0|94-preInit
    /**
     * Returns an initiliazed instance of alert1 component.
     * @return the initialized component instance
     */
    public Alert getAlert1() {
        if (alert1 == null) {//GEN-END:|94-getter|0|94-preInit
            // write pre-init user code here
            alert1 = new Alert("Aviso!");//GEN-BEGIN:|94-getter|1|94-postInit
            alert1.addCommand(getCmdVoltar());
            alert1.setCommandListener(this);
            alert1.setTimeout(Alert.FOREVER);//GEN-END:|94-getter|1|94-postInit
            // write post-init user code here
        }//GEN-BEGIN:|94-getter|2|
        return alert1;
    }
    //</editor-fold>//GEN-END:|94-getter|2|

    //<editor-fold defaultstate="collapsed" desc=" Generated Getter: cmdVoltar ">//GEN-BEGIN:|29-getter|0|29-preInit
    /**
     * Returns an initiliazed instance of cmdVoltar component.
     * @return the initialized component instance
     */
    public Command getCmdVoltar() {
        if (cmdVoltar == null) {//GEN-END:|29-getter|0|29-preInit
            // write pre-init user code here
            cmdVoltar = new Command("Voltar", Command.BACK, 0);//GEN-LINE:|29-getter|1|29-postInit
            // write post-init user code here
        }//GEN-BEGIN:|29-getter|2|
        return cmdVoltar;
    }
    //</editor-fold>//GEN-END:|29-getter|2|

    //<editor-fold defaultstate="collapsed" desc=" Generated Getter: choiceGroup1 ">//GEN-BEGIN:|107-getter|0|107-preInit
    /**
     * Returns an initiliazed instance of choiceGroup1 component.
     * @return the initialized component instance
     */
    public ChoiceGroup getChoiceGroup1() {
        if (choiceGroup1 == null) {//GEN-END:|107-getter|0|107-preInit
            // write pre-init user code here
            choiceGroup1 = new ChoiceGroup("Terminal", Choice.MULTIPLE);//GEN-LINE:|107-getter|1|107-postInit
            // write post-init user code here
        }//GEN-BEGIN:|107-getter|2|
        return choiceGroup1;
    }
    //</editor-fold>//GEN-END:|107-getter|2|

    //<editor-fold defaultstate="collapsed" desc=" Generated Getter: choiceGroup2 ">//GEN-BEGIN:|108-getter|0|108-preInit
    /**
     * Returns an initiliazed instance of choiceGroup2 component.
     * @return the initialized component instance
     */
    public ChoiceGroup getChoiceGroup2() {
        if (choiceGroup2 == null) {//GEN-END:|108-getter|0|108-preInit
            // write pre-init user code here
            choiceGroup2 = new ChoiceGroup("\u00D4nibus", Choice.MULTIPLE);//GEN-LINE:|108-getter|1|108-postInit
            // write post-init user code here
        }//GEN-BEGIN:|108-getter|2|
        return choiceGroup2;
    }
    //</editor-fold>//GEN-END:|108-getter|2|

    //<editor-fold defaultstate="collapsed" desc=" Generated Getter: spacer1 ">//GEN-BEGIN:|111-getter|0|111-preInit
    /**
     * Returns an initiliazed instance of spacer1 component.
     * @return the initialized component instance
     */
    public Spacer getSpacer1() {
        if (spacer1 == null) {//GEN-END:|111-getter|0|111-preInit
            // write pre-init user code here
            spacer1 = new Spacer(16, 1);//GEN-LINE:|111-getter|1|111-postInit
            // write post-init user code here
        }//GEN-BEGIN:|111-getter|2|
        return spacer1;
    }
    //</editor-fold>//GEN-END:|111-getter|2|

    //<editor-fold defaultstate="collapsed" desc=" Generated Getter: stringItem ">//GEN-BEGIN:|114-getter|0|114-preInit
    /**
     * Returns an initiliazed instance of stringItem component.
     * @return the initialized component instance
     */
    public StringItem getStringItem() {
        if (stringItem == null) {//GEN-END:|114-getter|0|114-preInit
            // write pre-init user code here
            stringItem = new StringItem("Hor\u00E1rio dos \u00F4nibus a pesquisar:", null, Item.PLAIN);//GEN-LINE:|114-getter|1|114-postInit
            // write post-init user code here
        }//GEN-BEGIN:|114-getter|2|
        return stringItem;
    }
    //</editor-fold>//GEN-END:|114-getter|2|

    //<editor-fold defaultstate="collapsed" desc=" Generated Getter: dateField ">//GEN-BEGIN:|115-getter|0|115-preInit
    /**
     * Returns an initiliazed instance of dateField component.
     * @return the initialized component instance
     */
    public DateField getDateField() {
        if (dateField == null) {//GEN-END:|115-getter|0|115-preInit
            // write pre-init user code here
            dateField = new DateField("Sa\u00EDda de:", DateField.TIME);//GEN-BEGIN:|115-getter|1|115-postInit
            dateField.setDate(new java.util.Date(54000000l));//GEN-END:|115-getter|1|115-postInit
            // write post-init user code here
        }//GEN-BEGIN:|115-getter|2|
        return dateField;
    }
    //</editor-fold>//GEN-END:|115-getter|2|

    //<editor-fold defaultstate="collapsed" desc=" Generated Getter: dateField1 ">//GEN-BEGIN:|116-getter|0|116-preInit
    /**
     * Returns an initiliazed instance of dateField1 component.
     * @return the initialized component instance
     */
    public DateField getDateField1() {
        if (dateField1 == null) {//GEN-END:|116-getter|0|116-preInit
            // write pre-init user code here
            dateField1 = new DateField("at\u00E9:", DateField.TIME);//GEN-BEGIN:|116-getter|1|116-postInit
            dateField1.setDate(new java.util.Date(54060000l));//GEN-END:|116-getter|1|116-postInit
            // write post-init user code here
        }//GEN-BEGIN:|116-getter|2|
        return dateField1;
    }
    //</editor-fold>//GEN-END:|116-getter|2|



    /**
     * Returns a display instance.
     * @return the display instance.
     */
    public Display getDisplay() {
        return Display.getDisplay(this);
    }

    /**
     * Exits MIDlet.
     */
    public void exitMIDlet() {
        switchDisplayable(null, null);
        destroyApp(true);
        notifyDestroyed();
    }

    /**
     * Called when MIDlet is started.
     * Checks whether the MIDlet have been already started and initialize/starts or resumes the MIDlet.
     */
    public void startApp() {
        if (midletPaused) {
            resumeMIDlet();
        } else {
            initialize();
            startMIDlet();
        }
        midletPaused = false;
    }

    /**
     * Called when MIDlet is paused.
     */
    public void pauseApp() {
        midletPaused = true;
    }

    /**
     * Called to signal the MIDlet to terminate.
     * @param unconditional if true, then the MIDlet has to be unconditionally terminated and all resources has to be released.
     */
    public void destroyApp(boolean unconditional) {
    }

    public void adicionarHorario() {
        Onibus onibus = new Onibus();
        Horario horario = new Horario();

        onibus.setNomeOnibus(popupOnibus.getString(choiceTipo.getSelectedIndex()));
        onibus.carrega();

        horario.setHoraSaida(textFieldHoraSaida.getDate());
        horario.setTipo(choiceTipo.getString(choiceTipo.getSelectedIndex()));

        onibus.adicionaHorario(horario);

        onibus.save();

        /*c1.setTime(objetoHorario.getHorario());
        c2.setTime(horarioComp);
        String time = c1.get(Calendar.HOUR_OF_DAY) + ":" +
        c1.get(Calendar.MINUTE) + ":" +
        c1.get(Calendar.SECOND);
        String time2 = c2.get(Calendar.HOUR_OF_DAY) + ":" +
        c2.get(Calendar.MINUTE) + ":" +
        c2.get(Calendar.SECOND);
        //getAlert().setString(time + "---" + time2);
        if (c1.get(Calendar.HOUR_OF_DAY) == c2.get(Calendar.HOUR_OF_DAY) - 2){
        getAlert().setString(String.valueOf(c1.get(Calendar.MINUTE)));
        }
        else{
        //getAlert().setString("false");
        }*/

    }

    public void adicionarOnibus() {
        Onibus onibus = new Onibus();
        onibus.setNomeOnibus(getFieldNomeOnibus().getString());
        onibus.setOrigem(getFieldOrigem().getString());
        onibus.setDestino(getFieldDestino().getString());
        onibus.save();

    }

    private void listarDados(Filtro filtro) {
        ObjectSet os = null;
        try {
            os = PersistableManager.getInstance().find(Onibus.class, filtro, null);
            if (os.size() > 0) {
                for (int i = 0; i < os.size(); i++) {
                    Onibus onibus = (Onibus) os.get(i); //obtem as instancias do banco
                    listaHorarios.append(onibus.toString(), null);
                    for (int j = 0; j < onibus.getListaHorarios().size(); j++) {
                        listaHorarios.append(((Horario) onibus.getListaHorarios().elementAt(j)).toString(), null);
                    }
                }
                //if (onibus.getOrigem().equalsIgnoreCase(filtro) || filtro == null ){

            }
        } catch (FloggyException ex) {
            ex.printStackTrace();
        }
    }

    public void itemStateChanged(Item item) {
        if (item == popupOnibus) {
            Onibus onibus = new Onibus();
            onibus.setNomeOnibus(popupOnibus.getString(popupOnibus.getSelectedIndex()));
            onibus.carrega();
            textFieldOrigem.setText(onibus.getOrigem());
            textFieldDestino.setText(onibus.getDestino());
        }
    }
}
